//
//  UPNavigationController.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/17.
//

#import "UPNavigationController.h"
#import "UIColor+Hex.h"

@interface UPNavigationController ()

@property(nonatomic, assign) BOOL pushing;

@end

@implementation UPNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        
        // Set back button color
        NSDictionary *buttonAttributes = @{
            NSForegroundColorAttributeName: [UIColor clearColor],
            NSFontAttributeName: [UIFont systemFontOfSize:16]
        };
        appearance.buttonAppearance.normal.titleTextAttributes = buttonAttributes;
        
        // Set back button image
        UIImage *backImage = [UIImage imageNamed:@"icon_back"];
        [appearance setBackIndicatorImage:backImage transitionMaskImage:backImage];
        [appearance configureWithOpaqueBackground];
        appearance.backgroundColor = [UIColor clearColor]; // Background color
        appearance.shadowColor = [UIColor clearColor];
        
        // Set title color
        NSDictionary *titleAttributes = @{
            NSForegroundColorAttributeName: [UIColor whiteColor]
        };
        appearance.titleTextAttributes = titleAttributes;
        
        // Apply to navigation bar
        UINavigationBar.appearance.standardAppearance = appearance;
        UINavigationBar.appearance.scrollEdgeAppearance = appearance; // For scroll edge scenarios
        UINavigationBar.appearance.tintColor = [UIColor colorWithHexString:@"#FFFFFF"];
    } else {
        // For iOS versions below 13.0
        UINavigationBar.appearance.barTintColor = [UIColor cyanColor]; // Background color
        
        NSDictionary *titleAttributes = @{
            NSForegroundColorAttributeName: [UIColor whiteColor]
        };
        UINavigationBar.appearance.titleTextAttributes = titleAttributes;
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.pushing)
    {
        return;
    }
    [super pushViewController:viewController animated:animated];
    self.pushing = YES;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.pushing = NO;
    });
}

@end
